/*
 * @Author: xmily
 * @Date: 2022-03-16 17:27:54
 * @LastEditors: xmily
 * @LastEditTime: 2023-02-05 23:34:55
 * @FilePath: \glxxapi\routes\comm.js
 * @Description:
 * 手机微信端及PC端通用API
 * Copyright (c) 2022 by xmily/mxthink.com.cn, All Rights Reserved.
 */
const router = require("koa-router")();
const siteBaseInfo = require("../models/sitebaseinfo.js");
const commMethod=require('../cfg/commMethod.js')
const sitecfg = require("../cfg/siteconfig.js");
const base = require("../models/Fjxx.js");
const aftclassrv_addstatus = require("../models/AfterClassService/addstatus.js");
router.prefix("/comm");
var term=""
var termlist=[]
router.use(async (ctx, next) => {
  termlist = await commMethod.getTermList();
  term = termlist.sort()[termlist.length - 1];
  await next();
});

//获取学期数据
router.get("/getTerm", async (ctx, next) => {
  // try {
  //   let result = await commMethod.getTermList();    
  //   ctx.body = { error: false, result: result };
  // } catch (err) {
  //   throw new Error("获取后台学期数据时出错:[" + err + "]");
  // }
  ctx.body = { error: false, result: termlist };
});
//获取站点基本信息
router.get("/getSiteBaseInfo", async (ctx, next) => {
  try {
    let query = [
      {
        $match: {
          schoolname: sitecfg.schoolname,
          term: term,
        },
      },
    ];
    let res = await siteBaseInfo.myAggregate(query, null, null);
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});
//获取全校班级信息
router.get("/getAllClassName", async (ctx, next) => {
  try {
    // let query = [
    //   {
    //     $match: {
    //       schoolname: sitecfg.schoolname,
    //       term: sitecfg.term,
    //     },
    //   },
    //   {
    //     $unwind: "$classno",
    //   },
    //   {
    //     $sort: {
    //       sq: 1,
    //     },
    //   },
    // ];
    // let res = await siteBaseInfo.myAggregate(query, null, null);
    let term2=ctx.query.term||term;
    let res=await commMethod.getClassList(term2)
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//获取全校教师
router.get("/getAllTeacher", async (ctx, next) => {
  try {
    let query = {
      accountType: "教师",
    };
    let fields = {
      username: 1,
      classno: 1,
      _id: 0,
    };
    let res = await base.myFindByQuery(query, fields);
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//用户登录入口
// router.post('/login', async (ctx, next) => {
//     let un = ctx.request.body.username;
//     let up = Enpassword(ctx.request.body.password);
//     try {
//         var accts = await base.myAggregate([
//             {
//                 $match: {
//                     username:un,
//                     password2:up
//                 }
//             }, {
//                 $lookup: {
//                     from: "baseExtendInfo",
//                     localField: "_id",
//                     foreignField: "baseid",
//                     as: "baseExtendInfo"
//                 }
//             }, {
//                 $unwind: {path:"$baseExtendInfo",preserveNullAndEmptyArrays:true}
//             },  {
//                 $project: {
//                     _id: 1,
//                     username: 1,
//                     gender: 1,
//                     wxopenid: 1,
//                     accountType:1,
//                     "baseExtendInfo.班级":1
//                 }
//             }
//         ])
//         if (accts.length == 0) {
//             ctx.body = {
//                 "error": true,
//                 "message": "登录失败，请检查用户名和密码是否正确！"
//             }
//             return;
//         }
//         var userinfo = {
//             username: accts[0].username,
//             id: accts[0]._id,
//             admin: accts[0].accountType,
//             classno: accts[0].accountType=="学生"||accts[0].accountType=="毕业生" ? accts[0].baseExtendInfo.班级?accts[0].baseExtendInfo.班级 : '非学生用户':'非学生用户'
//         };
//         let token = jwt.sign(userinfo, "mxthink")
//         ctx.body = {
//             "error": false,
//             'userinfo': userinfo,
//             userotherinfo: accts[0],
//             "token": token
//         }
//     } catch (err) {
//         ctx.body = {
//             'error': true,
//             "message": "登录失败:" + err.message
//         }
//     }
// })

module.exports = router;
